<?php

namespace App\Http\Resources;

use App\Models\PmFormItem;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PmFormItemResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request              $request
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'form_id' => $this->form_id,
            'title' => $this->title,
            'filed_name' => $this->field_name,
            'form_item_type' => $this->form_item_type,
            'form_item_type_text' => PmFormItem::$itemTypeMapping[$this->form_item_type] ?? '未知',
            'placeholder' => $this->placeholder,
            'options' => $this->json_content ? array_values(json_decode($this->json_content, 1)) : null,
            'is_required' => $this->is_required,
        ];
    }
}
